package com.huayun.plugin.pointcloud.controller;

import com.huayun.plugin.pointcloud.service.PointCloudService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@Tag(name = "点云拟合")
@RestController
@RequestMapping("/pointCloud")
public class PointCloudController {
    private final PointCloudService pointCloudService;
    public PointCloudController(PointCloudService pointCloudService) {
        this.pointCloudService = pointCloudService;
    }

    @Operation(summary = "创建并下载网格文件")
    @PostMapping("/downloadMeshGridFile")
    public StreamingResponseBody downloadMeshGridFile(@RequestParam String userName,
                                                      @RequestParam double searchRadius,
                                                      @RequestParam boolean isAutoSearchRadius,
                                                      @RequestBody MultipartFile pcdFile,
                                                      HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;");
        return outputStream -> pointCloudService.createMeshGridFile(userName, searchRadius, isAutoSearchRadius, pcdFile, outputStream);
    }
}
